package u1project.server;

import u1project.common.Request;
import u1project.common.Response;

import java.io.*;
import java.lang.reflect.Method;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class ServerThread implements Runnable {
    private final Socket s;

    public ServerThread(Socket s) {
        this.s = s;
    }

    public Response login(Request request) {
        String u = (String) request.get("username");
        String p = (String) request.get("userpass");
        if (u.equals("admin") && p.equals("laoliu")) {
            return new Response(true);
        }
        return new Response(false, "错误的用户名或者密码");
    }

    public Response reg(Request request) {
        String u = (String) request.get("username");
        String p = (String) request.get("userpass");
        String p2 = (String) request.get("userpass2");
        if (u.length() < 2 || p.length() < 6) {
            return new Response(false, "太短了");
        }
        if (!p.equals(p2)) {
            return new Response(false, "两次密码不一致");
        }
        if (u.equals("admin")) {
            return new Response(false, "用户名重复了");
        }
        return new Response(true);
    }

    public Response upload(Request request) {

        String novelName = (String) request.get("novelname");
        byte[] b = (byte[]) request.get("content");
        try {
            File f = new File("E:/temp", novelName);

            FileOutputStream fout = new FileOutputStream(f);
            fout.write(b);
            fout.close();
            return new Response(true, "此小说上传成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Response(false, "此小说" + novelName + "信息有错误");
    }

    //下载
    public Response download(Request request) {
        String novelName = (String) request.get("novelname");
        try {
            FileInputStream fin = new FileInputStream(new File("E:/temp", novelName));
            byte b[] = new byte[fin.available()];
            fin.read(b);
            fin.close();
            Response resp = new Response(true, "ok");
            resp.put("content", b);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Response(false, "此小说不存在");
    }

    public Response exit(Request request) {
        return new Response(true, "安全退出");
    }

    @Override
    public void run() {
        System.out.println(s + "连接成功！");
        ObjectInputStream oin = null;
        ObjectOutputStream oout = null;
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            oin = new ObjectInputStream(in);
            oout = new ObjectOutputStream(out);
            while (true) {
                Request request = (Request) oin.readObject();
                System.out.println("获得信息：" + request);
                Response resp = null;
                Method m = this.getClass().getDeclaredMethod(request.getAction(), Request.class);
                resp = (Response) m.invoke(this, request);
//                if (request.getAction().equals("login")) {
//                    resp = login(request);
//                } else if (request.getAction().equals("reg")) {
//                    resp = reg(request);
//                } else if (request.getAction().equals("upload")) {
//                    resp = upload(request);
//                } else if (request.getAction().equals("download")) {
//                    resp = download(request);
//                } else if (request.getAction().equals("exit")) {
//                    resp = exit(request);
//                } else {
//                    resp = new Response(false, "未支持的操作");
//                }
                //返回给客户端
                System.out.println("返回：" + resp);
                oout.writeObject(resp);

                oout.reset();
                oout.flush();

                if (request.getAction().equals("exit")) {
                    System.out.println(s + "优雅告别！");
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            //??>>
            try {
                if (s.isConnected()) {
                    oout.writeObject(new Response(false, "出现了异常：" + e));
                    oout.reset();
                    oout.flush();
                }
            } catch (IOException e1) {
                //e1.printStackTrace();
            }
        } finally {
            try {
                s.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
